﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutomationTools.Procedimentos.Acao.Transformacao;
using AutomationTools;
using AutomationTools.Procedimentos;
using System.Collections;
using AutomationTools.Procedimentos.Leitura;
using AutomationTools.Procedimentos.Leitura.Helper;
using AutomationTools.Procedimentos.Condicao;

namespace AutomationToolsSample
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            ProcedimentoController controller = new ProcedimentoController(
                new Request("http://www.magiccards.com.br", null, Request.EnumFormMethod.POST),
                new AutomationTools.Procedimentos.FormsHelper.PreencherComboBox(new InputSelect("buscar_em"), cmbBuscaEm),
                new AutomationTools.Procedimentos.FormsHelper.PreencherComboBox(new InputSelect("categoria"), cmbCategoria),
                new AutomationTools.Procedimentos.FormsHelper.PreencherComboBox(new InputSelect("info1"), cmbInfo1),
                new AutomationTools.Procedimentos.FormsHelper.PreencherComboBox(new InputSelect("info2"), cmbInfo2)
            );

            var ret = controller.ExecutarProcedimentos();
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {

            ParametrosRequest nill = new ParametrosRequest(
                new ParametroRequest(cmbBuscaEm.Tag.ToString(), ((Option)cmbBuscaEm.SelectedItem).Value),
                new ParametroRequest(cmbCategoria.Tag.ToString(), ((Option)cmbCategoria.SelectedItem).Value),
                new ParametroRequest(cmbInfo1.Tag.ToString(), ((Option)cmbInfo1.SelectedItem).Value),
                new ParametroRequest(cmbInfo2.Tag.ToString(), ((Option)cmbInfo2.SelectedItem).Value),
                new ParametroRequest("buscar_por", txtPesquisa.Text)
            );

            ProcedimentoController controller = new ProcedimentoController(
                new Request("http://www.magiccards.com.br/pesquisar.asp", nill, Request.EnumFormMethod.GET),
                new AutomationTools.Procedimentos.Acao.Transformacao.RegexParseObject<Resultado>()
            );

            var ret = (Resultado)controller.ExecutarProcedimentos();

            lblResult.Text = "";
            foreach (var item in ret.Cards.Where(x => x.Nome != null))
            {
                lblResult.Text += item.Nome.Replace("\r", "").Replace("\t", "").Replace("\n", "") + "-->" + item.Preco + Environment.NewLine;
            }
        }


        [CanRegexParse]
        public class Resultado
        {
            [RegexParse(@"<tr><td align=""center"">.*?</tr>", IsArray = true)]
            public Cartas[] Cards { get; set; }
        }

        [CanRegexParse]
        public class Cartas
        {
            [RegexParse(@"<a href="".*?>([^<]+?)</a>", Grupo = 1)]
            public String Nome { get; set; }

            [RegexParse(@"(R\$.*?)<", Grupo = 1)]
            public String Preco { get; set; }
        }

    }
}
